package com.quicklife.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.groovy.JsonGroovyBuilder;

import com.quicklife.service.inter.HeartService;
import com.quicklife.util.JsonUtil;
import com.quicklife.util.SqlHelper;
import com.quicklife.util.StringUtil;

/**
 * 心跳服务
 * 
 * @author HackerD
 * 
 */
public class HeartServiceImpl implements HeartService {

	@Override
	public String getPushCouponMessage(String rqinfos) {
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String lon = jsonObject.getString("lon");
			String lat = jsonObject.getString("lat");
			String userId = jsonObject.getString("userid");

			String sql = "select * from (select a.id,b.name busname,a.msg,a.couponid from message a "
					+ "inner join business b on  a.fromid=b.id "
					+ "where (a.type=1 or (a.type=2 and distanceoftwopoints(b.lon,b.lat,?,?)<=a.area)) "
					+ "and (select count(*) from pushmessage where msgid=a.id and userid=?)=0 and a.status=0) "
					+ "where rownum<4";

			List list = SqlHelper.executeQuery(sql, new String[] { lon, lat,
					userId }, null);
			if (list.size() > 0) {
				jsonObject = JSONObject.fromObject(JsonUtil.createRtJsonStr(
						StringUtil.RESULT_CODE_OK, list));
				JSONArray jsonArray = jsonObject.getJSONArray("rtinfos");
				Iterator<JSONObject> it = jsonArray.iterator();
				JSONObject object = null;
				while (it.hasNext()) {
					object = it.next();
					SqlHelper
							.executeUpdate(
									"insert into pushmessage(msgid,userid) values(?,?)",
									new String[] { object.getString("id"),
											userId });
				}
				return jsonObject.toString();
			}

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, null);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL, null);
		}
	}

	@Override
	public String getFriendRequest(String rqinfos) {
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String userId = jsonObject.getString("userid");

			String sql = "select a.id,a.msg from message a   where toid=? and "
					+ "(select count(*) from pushmessage where msgid=a.id)=0 "
					+ "and a.status=0 and a.type=3";

			List list = SqlHelper.executeQuery(sql, new String[] { userId },
					null);
			if (list.size() > 0) {
				jsonObject = JSONObject.fromObject(JsonUtil.createRtJsonStr(
						StringUtil.RESULT_CODE_OK, list));
				JSONArray jsonArray = jsonObject.getJSONArray("rtinfos");
				Iterator<JSONObject> it = jsonArray.iterator();
				JSONObject object = null;
				while (it.hasNext()) {
					object = it.next();
					SqlHelper
							.executeUpdate(
									"insert into pushmessage(msgid,userid) values(?,?)",
									new String[] { object.getString("id"),
											userId });
				}
				return jsonObject.toString();
			}

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, null);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL, null);
		}
	}

	@Override
	public String appUpdate(String rqinfos) {
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String version = jsonObject.getString("version");
			String maxVersion = null;

			String sql = "select version from appupdate where id=(select max(id) from appupdate)";
			List list = SqlHelper.executeQuery(sql, null, null);
			if (list.size() > 0) {
				maxVersion = (String) ((Map) list.get(0)).get("version");
				if (!version.equalsIgnoreCase(maxVersion)) {
					return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK,
							maxVersion);
				}
			}

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL, null);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

}
